package com.powerunion.datacollection.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.RoleDao;
import com.powerunion.datacollection.model.Column;
import com.powerunion.datacollection.model.Role;
import com.powerunion.datacollection.model.TargetDetails;
import com.powerunion.datacollection.model.UsersRoles;
import com.powerunion.datacollection.model.jsonObject.JSONDataBean;
import com.powerunion.util.log.LogHelper;


public class RoleManager {
	// 初始化日志类
	private static final Logger logger = LogHelper.getInstance().getLogger(
			RoleManager.class);
	
	private RoleDao roleDao = null;
	
	public RoleManager() {
		super();
		try {
			this.roleDao = (RoleDao)DaoFactory.getInstance().getDao(RoleDao.class);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public boolean save(String roleStr,String roleUsers) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(roleStr)) {
			String[] args = { "roleStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter roleStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//参数为空
		if(StringUtils.isEmpty(roleUsers)) {
			String[] args = { "roleUsers" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter roleUsers NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//转换成JSON对象
		JSONObject jsonObject = JSONObject.fromObject(roleStr);
		Role role = (Role)JSONObject.toBean(jsonObject,Role.class);

		String roleId ="";
		roleId = this.roleDao.save(role);
		
		Map map = new HashMap();
		map.put("data", UsersRoles.class);

		JSONDataBean cl = (JSONDataBean)JSONObject.toBean(JSONObject.fromObject(roleUsers), JSONDataBean.class,map);
		
		UsersRolesManager um = new UsersRolesManager();
		
		return um.save(cl.getData(), roleId);
		
	}
	
	public boolean delete(String code) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(code)) {
			String[] args = { "code" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter code NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		//删除该角色下的用户或组
		UsersRolesManager urm = new UsersRolesManager();
		urm.delete(code);
		String[]codes = new String[1];
		codes[0] = code;
		return this.roleDao.delete(codes);
	}
	
	public String GetRoles() throws Exception {
		List roles = this.roleDao.findRoles();
		JSONArray jsonArr = JSONArray.fromObject(roles);
		String result = "{results:"+jsonArr+"}";
		return result;
	}
	
	public Role findRoleByKey(String id) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.roleDao.findRoleByKey(id);
	}
	
	public JSONArray getUserTree(String paraSeq,int type) throws Exception {
		return this.roleDao.getUserTree(paraSeq, type);
	}
	
	public JSONArray getRoleTree() throws Exception {
		return this.roleDao.getRoleTree();
	}
	
	
}
